from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.areas.models import Area
from utils.response_code import RETCODE


class AresView(View):
    def get(self, request):
        #获取省份id
        area_id = request.GET.get('area_id')
        if area_id is None:
            pro_id = Area.objects.filter(parent__isnull=True)
            pro_list = []
            for pro in pro_id:
                pro_list.append({
                    'id': pro.id,
                    'name': pro.name
                })
            return http.JsonResponse({'code':RETCODE.OK,'errmsg':'OK','province_list':pro_list})
        else:
            #获取市 所有的县
            parent_model = Area.objects.filter(parent_id=area_id)
            print(parent_model)
            sub_list = []
            for sub_model in parent_model:
                sub_list.append({
                    'id': sub_model.id,
                    'name': sub_model.name
                })
            return http.JsonResponse({'code':RETCODE.OK,'errmsg':'OK','sub_data':sub_list})





